package cn.pconline.autoclub.service;

import java.util.Date;

import org.gelivable.auth.entity.GeliSession;
import org.gelivable.auth.entity.GeliUser;
import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import cn.pconline.autoclub.config.SystemConfig;
import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubLevel;
import cn.pconline.autoclub.entity.UpgradeGift;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.r.client.RClient;
import cn.pconline.r.client.SimpleHttpTemplate;

/**
 * Service-车友会升星礼品奖励
 * @author zhoutianhuai
 */
public class UpgradeGiftService {

    @Autowired
    private GeliDao geliDao;
    @Autowired
    private GeliOrm geliOrm;
    @Autowired
    private PagerService pagerService;
    @Autowired
    private UserService userService;
    @Autowired
    private ClubService clubService;
    @Autowired
    private SystemConfig systemConfig;
    @Autowired
    private RClient rClient;
    @Autowired
    private SimpleHttpTemplate simpleHttpTemplate;
    private static final Logger log = LoggerFactory.getLogger(UpgradeGiftService.class);
    
    public long createUpgradeGift(UpgradeGift upgradeGift) {
        GeliUser user = GeliSession.getCurrentUser();
        if (user != null) {
        	upgradeGift.setCreateBy(user.getUserId());
        }
        upgradeGift.setCreateAt(new Date());
        return geliDao.create(upgradeGift);
    }

    public int updateUpgradeGift(UpgradeGift upgradeGift) {
    	upgradeGift.setUpdateAt(new Date());
        GeliUser u = GeliSession.getCurrentUser();
        long uid = 0;
        if (u != null) {
            uid = u.getUserId();
        }
        upgradeGift.setUpdateBy(uid);
        return geliDao.update(upgradeGift);
    }
    
    
    public UpgradeGift findUpgradeGift(long id) {
        try {
            return geliDao.find(UpgradeGift.class, id);
        } catch (DataAccessException ex) {
            return null;
        }
    }
    
    public Pager<UpgradeGift> pageUpgradeGift(int pageNo, int pageSize, long levelId) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select gift_id from ").appendSql(geliOrm.getTableName(UpgradeGift.class)).appendSql(" where  1=1");
        sql.appendSql(" and status = ").appendValue(UpgradeGift.STATUS_UP);
        sql.appendSql(" and cur_num >0 ");
        if(levelId == ClubLevel.LEVEL_ONE){
            sql.appendSql(" order by is_required desc, value_point, seq ,create_at desc");
        }else{
        	sql.appendSql(" order by value_point, seq ,create_at desc");
        }
        return pagerService.pager(UpgradeGift.class, sql, pageNo, pageSize);
    }
    
    public Pager<UpgradeGift> pageUpgradeGiftForAdmin(int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select gift_id from ").appendSql(geliOrm.getTableName(UpgradeGift.class)).appendSql(" where  1=1");
        sql.appendSql(" and status != ").appendValue(UpgradeGift.STATUS_DELETE);
        sql.appendSql(" order by seq ,create_at desc");
        return pagerService.pager(UpgradeGift.class, sql, pageNo, pageSize);
    }
   
}
